<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>对账统计</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="{{asset('/layuiadmin/layui/css/layui.css')}}" media="all">
  <link rel="stylesheet" href="{{asset('/layuiadmin/style/admin.css')}}" media="all">
  <style>
  .yname{
          font-size: 13px;
          color: #444;
        },
/* .layui-col-md4:style="width:26%"hover{
        border:5px;
        width:24%;
        transition: all 0.3s ease 0s;
        } */
  .bj{
    width:45px;
    height:45px;
  }
  .logo{
      width:45px;
      height:45px;
    }
  </style>
</head>
<body>
 
  <div class="layui-fluid">
    <!-- 筛选------------------------------------------------------------ -->
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card"style="margin-top:50px"> 
          <div class="layui-card-header">对账统计</div>

          <div class="layui-card-body">
            <div class="layui-btn-container" style="font-size:14px;">
             
                                  
              <!-- 缴费时间 -->
            <div class="layui-form" style="display: flex;width:100%">                      
                 <!-- 选择业务员 -->
              <div class="layui-form" lay-filter="component-form-group" style="width:190px;margin-right:10px;display: inline-block;">
                <div class="layui-form-item">                          
                  <div class="layui-input-block" style="margin-left:0">
                  <text class="yname">选择门店</text>
                      <select name="agent" id="agent" lay-filter="agent" lay-search>
                          
                      </select>
                  </div>
                </div>
              </div>
              <!-- 学校 -->
              <div class="layui-form" lay-filter="component-form-group" style="width:190px;margin-right:10px;display: inline-block;">
                <div class="layui-form-item">                          
                  <div class="layui-input-block" style="margin-left:0">
                  <text class="yname">选择收银员</text>
                      <select name="schooltype" id="schooltype" lay-filter="schooltype" lay-search>
                          
                      </select>
                  </div>
                </div>
              </div>
                <!-- 支付状态 -->
              <div class="layui-form" lay-filter="component-form-group" style="width:200px;display: inline-block;">
                <div class="layui-form-item">                          
                  <div class="layui-input-block" style="margin-left:10px;">
                      <text class="yname">支付类型</text>
                      <select name="status" id="status" lay-filter="status">
                        <option value="">全部</option>
                        <option value="1">微信</option>
                        <option value="2">支付宝</option>
                        <option value="3">翼支付</option>
                        <option value="4">云闪付</option>
                        <option value="5">刷卡</option>
                      </select>
                  </div>
                </div>
              </div>
              <!-- 通道类型 -->
              <div class="layui-form" lay-filter="component-form-group" style="width:200px;display: inline-block;">
                <div class="layui-form-item">                          
                  <div class="layui-input-block" style="margin-left:10px;">
                  <text class="yname">通道类型</text>
                      <select name="passway" id="passway" lay-filter="passway">
                        
                      </select>
                  </div>
                </div>
              </div>
              <div class="layui-form" lay-filter="component-form-group" style="width:200px;display: inline-block;">
                <div class="layui-form-item">                          
                  <div class="layui-input-block" style="margin-left:10px;">
                  <text class="yname">输入金额</text>
                    <input type="text" name="money" autocomplete="off" placeholder="请输入金额" class="layui-input money">
                  </div>
                </div>
              </div>
                <div class="layui-form-item">                          
                  <div class="layui-inline"style="margin-left:10px;">
                    
                    <div class="layui-input-inline">
                    <text class="yname">订单开始时间</text>
                      <input type="text" class="layui-input start-item test-item" placeholder="订单开始时间" lay-key="23">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <div class="layui-input-inline">
                    <text class="yname">订单结束时间</text>
                      <input type="text" class="layui-input end-item test-item" placeholder="订单结束时间" lay-key="24">
                    </div>
                  </div>
                </div>
              </div>  
              <div class="layui-form" lay-filter="component-form-group" style="width:500px;margin-right:10px;display: inline-block;">
                <div class="layui-form-item">                          
                  <div class="layui-input-block" style="margin-left:0">
                    <button class="layui-btn" style="border-radius:5px;" id="countsearch">搜索</button>
                     <button class="layui-btn" id="today"style="border-radius:5px">今日</button>
                     <button class="layui-btn" id="yesterday"style="border-radius:5px">昨日</button>
                     <!-- <button class="layui-btn" id="export"style="border-radius:5px">导出</button> -->
                  </div>
                </div>
              </div>               
            </div>  
          </div>
        </div>
      </div>
    </div>
    
    <!-- 统计数据------------------------------------------------------------ -->
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card" style="background-color: transparent;"> 
          <div class="layui-card-header">统计数据(*商家实收=交易金额-退款金额，实际净额=商家实收-结算手续费)</div>          
        </div>
      </div>
    </div>
    <div class="layui-row layui-col-space15">
      <div class="layui-col-sm6 layui-col-md4"style="width:26%">
        <div class="layui-card"style="background: url({{asset('/school/images/yesterday_back.png')}}) center center / cover no-repeat;border-radius: 5px;">
          <div class="layui-card-header"style="border-bottom:none">
            交易金额/笔数
            <span class="layui-badge layui-bg-orange layuiadmin-badge">元/笔</span>
          </div>
          <div class="layui-card-body layuiadmin-card-list">
            <p class="layuiadmin-big-font acounts"style="font-size:25px"></p>            
          </div>
        </div>
      </div>
      <div class="layui-col-sm6 layui-col-md4"style="width:26%">
        <div class="layui-card"style="background: url({{asset('/school/images/yesterday_back.png')}}) center center / cover no-repeat;border-radius: 5px;">
   
          <div class="layui-card-header"style="border-bottom:none">
            商家实收
            <span class="layui-badge layui-bg-blue layuiadmin-badge">元</span>
          </div>
          <div class="layui-card-body layuiadmin-card-list">
            <p class="layuiadmin-big-font acounts"style="font-size:25px"></p>            
          </div>
        </div>
      </div>
      <div class="layui-col-sm6 layui-col-md4"style="width:26%">
        <div class="layui-card"style="background: url({{asset('/school/images/yesterday_back.png')}}) center center / cover no-repeat;border-radius: 5px;">
          <div class="layui-card-header"style="border-bottom:none">
            实际净额
            <span class="layui-badge layui-bg-black layuiadmin-badge">元</span>
          </div>
          <div class="layui-card-body layuiadmin-card-list">
            <p class="layuiadmin-big-font acounts"style="font-size:25px"></p>            
          </div>
        </div>
      </div>
      
      <div class="layui-col-sm6 layui-col-md4"style="width:26%">
        <div class="layui-card"style="background: url({{asset('/school/images/yesterday_back.png')}}) center center / cover no-repeat;border-radius: 5px;">
          <div class="layui-card-header"style="border-bottom:none">
            总退款金额/笔数
            <span class="layui-badge layuiadmin-badge" style="background-color: #5FB878;color: #fff;">元/笔</span>
          </div>
          <div class="layui-card-body layuiadmin-card-list">
            <p class="layuiadmin-big-font acounts"style="font-size:25px"></p>            
          </div>
        </div>
      </div>
      <div class="layui-col-sm6 layui-col-md4"style="width:26%">
        <div class="layui-card"style="background: url({{asset('/school/images/yesterday_back.png')}}) center center / cover no-repeat;border-radius: 5px;">
          <div class="layui-card-header"style="border-bottom:none">
            隔日退款金额/笔数
            <span class="layui-badge layuiadmin-badge" style="background-color: #5FB878;color: #fff;">元/笔</span>
          </div>
          <div class="layui-card-body layuiadmin-card-list">
            <p class="layuiadmin-big-font acounts"style="font-size:25px"></p>            
          </div>
        </div>
      </div>
      <div class="layui-col-sm6 layui-col-md4"style="width:26%">
        <div class="layui-card"style="background: url({{asset('/school/images/yesterday_back.png')}}) center center / cover no-repeat;border-radius: 5px;">
          <div class="layui-card-header"style="border-bottom:none">
            手续费
            <span class="layui-badge layui-bg-red layuiadmin-badge">元</span>
          </div>
          <div class="layui-card-body layuiadmin-card-list">
            <p class="layuiadmin-big-font acounts"style="font-size:25px"></p>            
          </div>
        </div>
      </div>
      <div class="layui-col-sm6 layui-col-md4"style="width:26%">
        <div class="layui-card"style="background: url({{asset('/school/images/yesterday_back.png')}}) center center / cover no-repeat;border-radius: 5px;">
          <div class="layui-card-header"style="border-bottom:none">
            预充优惠金额/笔数
            <span class="layui-badge layui-bg-red layuiadmin-badge">元/笔</span>
          </div>
          <div class="layui-card-body layuiadmin-card-list">
            <p class="layuiadmin-big-font acounts"style="font-size:25px"></p>
          </div>
        </div>
      </div>
      <div class="layui-col-sm6 layui-col-md4"style="width:26%">
        <div class="layui-card"style="background: url({{asset('/school/images/yesterday_back.png')}}) center center / cover no-repeat;border-radius: 5px;">
          <div class="layui-card-header"style="border-bottom:none">
          免充优惠金额/笔数
            <span class="layui-badge layuiadmin-badge" style="background-color: #5FB878;color: #fff;">元/笔</span>
          </div>
          <div class="layui-card-body layuiadmin-card-list">
            <p class="layuiadmin-big-font acounts"style="font-size:25px"></p>            
          </div>
        </div>
      </div>
    </div>

  </div>
  <div id="main" style="width: 600px;height:400px;"></div>


  <input type="hidden" class="store_id">
  <input type="hidden" class="merchant_id">

  <input type="hidden" class="starttime"><!-- 今天的开始时间 -->
  <input type="hidden" class="endtime"><!-- 今天的开始时间 -->

  <input type="hidden" class="starttimeY"><!-- 昨天的开始时间 -->
  <input type="hidden" class="endtimeY"><!-- 昨天的结束时间 -->
  <input type="hidden" class="curHdIndex"><!-- 时间选择1 今日 2昨日 3 上周 4 本月 5 上月 0自选时间范围 -->
  <input type="hidden" class="pay_type"><!-- 时间选择1 今日 2昨日 3 上周 4 本月 5 上月 0自选时间范围 -->
  <input type="hidden" class="company_id">

<script src="{{asset('/layuiadmin/layui/layui.js')}}"></script>   
<script>
     var token = localStorage.getItem("Publictoken");
    
    layui.config({
      base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,form = layui.form
            ,table = layui.table
            ,laydate = layui.laydate;
      // 未登录,跳转登录页面
      // $(document).ready(function(){        
      //     if(token==null){
      //         window.location.href="{{url('/mb/login')}}"; 
      //     }
      // })
    // 获取时间
    var nowdate = new Date();
    // 今天
    var year=nowdate.getFullYear();
    var mounth=nowdate.getMonth()+1;
    var day=nowdate.getDate();
    var hour = nowdate.getHours();       
    var min = nowdate.getMinutes();     
    var sec = nowdate.getSeconds();
    if(mounth.toString().length<2 && day.toString().length<2){
        var nwedata = year+'-0'+mounth+'-0'+day+' '+'23'+':'+'59'+':'+'59';
    }else if(mounth.toString().length<2){
        var nwedata = year+'-0'+mounth+'-'+day+' '+'23'+':'+'59'+':'+'59';
    }else if(day.toString().length<2){
        var nwedata = year+'-'+mounth+'-0'+day+' '+'23'+':'+'59'+':'+'59';
    }else{
        var nwedata = year+'-'+mounth+'-'+day+' '+'23'+':'+'59'+':'+'59';
    }
    
    $('.end-item').val(nwedata);//今天的时间
    $('.endtime').val(nwedata);
    //今天的开始时间
    if(mounth.toString().length<2 && day.toString().length<2){
        var nwedatastart = year+'-0'+mounth+'-0'+day+' '+'00'+':'+'00'+':'+'00';
    }
    else if(mounth.toString().length<2){
        var nwedatastart = year+'-0'+mounth+'-'+day+' '+'00'+':'+'00'+':'+'00';
    }
    else if(day.toString().length<2){
        var nwedatastart = year+'-'+mounth+'-0'+day+' '+'00'+':'+'00'+':'+'00';
    }
    else{
        var nwedatastart = year+'-'+mounth+'-'+day+' '+'00'+':'+'00'+':'+'00';
    }
    $('.starttime').val(nwedatastart);
    $('.start-item').val(nwedatastart);
    // *******************************************************************************
    var years=nowdate.getFullYear();
    var mounths=nowdate.getMonth()+1;
    var days=nowdate.getDate()-1;
    //昨天的开始时间
    if(mounth.toString().length<2 && day.toString().length<2){
        var yesterdaystart = years+'-0'+mounths+'-0'+days+' '+'00'+':'+'00'+':'+'00';
    }
    else if(mounth.toString().length<2){
        var yesterdaystart = year+'-0'+mounths+'-'+days+' '+'00'+':'+'00'+':'+'00';
    }
    else if(day.toString().length<2){
        var yesterdaystart = years+'-'+mounths+'-0'+days+' '+'00'+':'+'00'+':'+'00';
    }
    else{
        var yesterdaystart = years+'-'+mounths+'-'+days+' '+'00'+':'+'00'+':'+'00';
    }

    if(mounth.toString().length<2 && day.toString().length<2){
        var yesterdayend = years+'-0'+mounths+'-0'+days+' '+'23'+':'+'59'+':'+'59';
    }
    else if(mounth.toString().length<2){
        var yesterdayend = years+'-0'+mounths+'-'+days+' '+'23'+':'+'59'+':'+'59';
    }
    else if(day.toString().length<2){
        var yesterdayend = years+'-'+mounths+'-0'+days+' '+'23'+':'+'59'+':'+'59';
    }
    else{
        var yesterdayend = years+'-'+mounths+'-'+days+' '+'23'+':'+'59'+':'+'59';
    }
    $('.starttimeY').val(yesterdaystart);
    $('.endtimeY').val(yesterdayend);

    var arr
    function acount(){
        // 对账查询
        var loading = layer.msg('数据请求中..', {
            icon: 16
            ,shade: 0.3
            ,time: false
        });
        let params = {
            token:token,
            store_id:$('.store_id').val(),
            merchant_id:$('.merchant_id').val(),
            pay_type:$('.pay_type').val(),
            company:$('.company_id').val(),
            total_amount:$('.money').val()
        }
        if($('.start-item').val() || $('.end-item').val()){
            params['curHdIndex'] = 0;
            let start_date = $('.start-item').val();
            let end_date = $('.end-item').val();
            params['startDate']= start_date && start_date.length <=10 ? start_date + ' 00:00:00' : start_date;
            params['endDate']= end_date && end_date.length <=10 ? end_date + ' 23:59:59' : end_date;
        }else{
            params['curHdIndex'] = $('.curHdIndex').val()
        }
        $.get("{{url('/api/v2/Bill/Merchant/merchantBiCount')}}",
        params,
        function(res){
          if (loading){
            layer.close(loading);
          }

         if(res.status =='1'){
              arr=res.data;
            $('.layui-col-space15 .layui-col-sm6').eq(0).find('.layui-card .acounts').html(res.data.total_fee+'/'+res.data.total_num);
            $('.layui-col-space15 .layui-col-sm6').eq(1).find('.layui-card .acounts').html(res.data.merchant_actual_receipt || 0.00);
          $('.layui-col-space15 .layui-col-sm6').eq(2).find('.layui-card .acounts').html(res.data.merchant_actual_amount || 0.00);
          $('.layui-col-space15 .layui-col-sm6').eq(3).find('.layui-card .acounts').html(res.data.refund_fee+'/'+res.data.refund_num);
          $('.layui-col-space15 .layui-col-sm6').eq(4).find('.layui-card .acounts').html(res.data.refund_fee_nextday+'/'+res.data.refund_num_next);
          $('.layui-col-space15 .layui-col-sm6').eq(5).find('.layui-card .acounts').html(res.data.fee_amount);
          $('.layui-col-space15 .layui-col-sm6').eq(6).find('.layui-card .acounts').html(res.data.coupon_amount_yc+'/'+res.data.coupon_num_yc);
          $('.layui-col-space15 .layui-col-sm6').eq(7).find('.layui-card .acounts').html(res.data.coupon_amount_mc+'/'+res.data.coupon_num_mc);

         }else{
              layer.alert(res.message, {icon: 2}); 
         }

          

        },"json").fail(function(xhr,status,errorThrown){
          if (loading){
            layer.close(loading);
          }
        });

      }



      // 选择门店
      $.ajax({
          url : "{{url('/api/merchant/store_lists')}}",
          data : {token:token,l:100},
          type : 'post',
          success : function(data) {
              console.log(data);
              var optionStr = "";
                  for(var i=0;i<data.data.length;i++){
                      optionStr += "<option value='" + data.data[i].store_id + "'>" + data.data[i].store_name + "</option>";
                  }    
                  $("#agent").append('<option value="">全部</option>'+optionStr);
                  layui.form.render('select');
          },
          error : function(data) {
              alert('查找板块报错');
          }
      });


       // 选择通道
    $.ajax({
        url : "{{url('/api/v2/Merchant/Merchant/store_open_pay_way_lists')}}",
        data : {token:token,l:100},
        type : 'get',
        dataType:'json',
        success : function(data) {
            console.log(data);
            var optionStr = "";
                for(var i=0;i<data.data.length;i++){
                    optionStr += "<option value='" + data.data[i].company + "'>"
                        + data.data[i].company_desc + "</option>";
                }    
                $("#passway").append('<option value="">选择通道类型</option>'+optionStr);
                layui.form.render('select');
        },
        error : function(data) {
            alert('查找板块报错');
        }
    });
     
      // 选择门店
      form.on('select(agent)', function(data){
        var store_id = data.value;
        $('.store_id').val(store_id);
        //执行重载
        // acount();
        // 选择收银员
        $.ajax({
            url : "{{url('/api/merchant/merchant_lists')}}",
            data : {token:token,store_id:store_id,l:100},
            type : 'post',
            success : function(data) {
                console.log(data);
                var optionStr = "";
                    for(var i=0;i<data.data.length;i++){
                        optionStr += "<option value='" + data.data[i].merchant_id + "'>"
                          + data.data[i].name + "</option>";
                    }    
                    $("#schooltype").html('');
                    $('.merchant_id').val('');
                    $("#schooltype").append('<option value="">全部</option>'+optionStr);
                    layui.form.render('select');
            },
            error : function(data) {
                alert('查找板块报错');
            }
        });

      });
      
      // 选择收银员
      form.on('select(schooltype)', function(data){
        var user_id = data.value;
        $('.merchant_id').val(user_id);
        //执行重载
        // acount();
      });
      // 选择支付类型
        form.on('select(status)', function(data){
        var store_id = data.value;
        $('.pay_type').val(store_id);
        //执行重载
          // acount()
        });
        // 选择通道类型
        form.on('select(passway)', function(data){
        var store_id = data.value;
        $('.company_id').val(store_id);
        //执行重载
          // acount()
        });

      laydate.render({
        elem: '.start-item'
        ,type: 'datetime'
        ,min: '2024-10-01 00:00:00' // 最小日期
        ,btns: ['clear', 'confirm']
        ,done: function(value){
          $('.start-item').val(value)
          //执行重载
          // acount();
        }
      });

      laydate.render({
        elem: '.end-item'
        ,type: 'datetime'
        ,min: '2024-10-01 00:00:00' // 最小日期
        ,btns: ['clear', 'confirm']
        ,ready: function(date){
          this.dateTime.hours=23;
          this.dateTime.minutes=59;
          this.dateTime.seconds=59;
        }
        ,done: function(value){
          $('.end-item').val(value)
          //执行重载
          // acount();
        }
      });

      $('#today').click(function(){
        $('.start-item').val($('.starttime').val())
        $('.end-item').val($('.endtime').val());
        $('.curHdIndex').val(1);
        acount();
      })

      $('#yesterday').click(function(){
        $('.start-item').val($('.starttimeY').val())
        $('.end-item').val($('.endtimeY').val())
        $('.curHdIndex').val(2);
        acount();
      })

      $('#countsearch').click(function(){
        acount();
      })
    //acount();
      

      $('#export').click(function(){
        // console.log(arr)
          var store_id=$('.store_id').val();
          var merchant_id=$('.merchant_id').val();
          let start_date = $('.start-item').val();
          let end_date = $('.end-item').val();
          var time_start = start_date && start_date.length <=10 ? start_date + ' 00:00:00' : start_date;
          var time_end = end_date && end_date.length <=10 ? end_date + ' 23:59:59' : end_date;

          var count_data=JSON.stringify(arr);


          window.location.href="{{url('/api/export/MerchantOrderCountExcelDown')}}"+"?token="+token+"&store_id="+store_id+"&merchant_id="+merchant_id+"&time_start="+time_start+"&time_end="+time_end+"&count_data="+count_data;     
      })

        
    });

  </script>

</body>
</html>